package org.jground.rx.ui.zk.viewControl;

import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.RXAttribute;
import org.jground.rx.RXType;
import org.jground.rx.RXView;
import org.jground.rx.ui.IUIAttribute;
import org.jground.rx.ui.zk.UIFrame;
import org.zkoss.zk.ui.Component;

public class UIAttributeFactory {

    private static Log logger = LogFactory.getLog(UIAttributeFactory.class);

    private static HashMap<String, IUIAttribute<Component,UIFrame>> attributeFactories = new HashMap<String, IUIAttribute<Component,UIFrame>>();

    public static void register(String key, IUIAttribute<Component,UIFrame> uiAttribute) {
        attributeFactories.put(key, uiAttribute);
    }

    static {
        attributeFactories.put(RXType.STRING, new UIAttributeSimple());
        attributeFactories.put(RXType.TEXT, new UIAttributeSimple());
        attributeFactories.put(RXType.HTMLTEXT, new UIAttributeSimple());
        attributeFactories.put(RXType.INTEGER, new UIAttributeSimple());
        attributeFactories.put(RXType.FLOAT, new UIAttributeSimple());
        attributeFactories.put(RXType.PERCENTAGE, new UIAttributeSimple());
        attributeFactories.put(RXType.DATE, new UIAttributeSimple());
        attributeFactories.put(RXType.DATETIME, new UIAttributeSimple());
        attributeFactories.put(RXType.TIME, new UIAttributeSimple());
        attributeFactories.put(RXType.CURRENCY, new UIAttributeSimple());
        attributeFactories.put(RXType.TUPLE, new UIAttributeTuple());
        attributeFactories.put(RXType.LIST, new UIAttributeList());
        attributeFactories.put(RXType.REF, new UIAttributeSimple());
        attributeFactories.put(RXType.LIST + "-" + RXType.REF, new UIAttributeComponent());
        attributeFactories.put(RXType.COMPONENT, new UIAttributeSimple());
        attributeFactories.put(RXType.LIST + "-" + RXType.COMPONENT, new UIAttributeComponent());
        attributeFactories.put(RXType.ONEOF, new UIAttributeOneof());
        attributeFactories.put(RXType.FOREACH, new UIAttributeForeach());
        attributeFactories.put(RXType.VALUES, new UIAttributeList());

        // createMethods.Put("object-reference",
        // UIAttributeObjectReference.Create);
        attributeFactories.put("DUMMY", new UIAttributeSimple());

    }

    public static IUIAttribute<Component,UIFrame> create(UIFrame uiFrame, RXView moView) {
        // View Ref
        //
        IUIAttribute<Component,UIFrame> uiAttribute = null;
        if (moView.getProperty("moidref") != null) {
            logger.info(moView);
        }
        String widgetId = moView.getWidgetHint();
        if (widgetId == null) {
            try {
                RXAttribute rxAtt = moView.getRXAttribute();
                if (rxAtt == null) {
                    String errMsg = "no RXAttribute found for RXView.attref: " + moView.getAttref();
                    logger.error(errMsg);
                    throw new RuntimeException(errMsg);
                }
                widgetId = moView.getRXAttribute().getRXType().getFullTypeId();
            } catch (Exception e) {
                logger.error(moView, e);
            }
        }
        logger.debug(widgetId);
        if (widgetId != null) {
            if (attributeFactories.containsKey(widgetId)) {
                uiAttribute = attributeFactories.get(widgetId);
            } else if (widgetId.startsWith(RXType.LIST)) {
                uiAttribute = attributeFactories.get(RXType.LIST);
            }
        } else {
            logger.warn("widgetId is null, moView: " + moView);
        }
        return uiAttribute == null ? attributeFactories.get("DUMMY").createNew(uiFrame, moView) : uiAttribute
                .createNew(uiFrame, moView);
    }

}